package com.yssoft.service.equipment;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.equipment.TEquipmentImgP;
import com.yssoft.entity.equipment.TEquipmentRecordImgP;
import com.yssoft.entity.equipment.TEquipmentRecordImgT;
import com.yssoft.entity.equipment.TEquipmentRecordPVO;
import com.yssoft.entity.equipment.TEquipmentRecordTVO;
import com.yssoft.mapper.core.TCodeMapper;
import com.yssoft.mapper.equipment.TEquipmentRecordImgTMapper;
import com.yssoft.mapper.equipment.TEquipmentRecordTMapper;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.UploadFile;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.Transient;


@Service
public class EquipmentRecordTService {
    @Resource
	private TEquipmentRecordTMapper equipmentRecordTMapper;
    @Resource
    private TEquipmentRecordImgTMapper equipmentRecordImgTMapper;
    @Resource
    private TCodeMapper codeMapper;
    
    public List<Map<String,Object>> pageList(Map<String,Object> params) {
    	List<Map<String, Object>> list = equipmentRecordTMapper.pageList1(params);
    	for (Map<String, Object> map : list) {
			map.put("pic", equipmentRecordImgTMapper.selectImg(Integer.valueOf(map.get("id").toString())));
		}
    	return list;
    }
    
    @Transient
    public int deleteOne(Integer id) {
    	
    	return this.equipmentRecordTMapper.deleteOne(id);
    }
    
    @Transient
    public int insertSelective(TEquipmentRecordTVO equipmentRecordTVO,MultipartRequest mRequest) {
    	int stat = 0;
    	try {
    		stat = this.equipmentRecordTMapper.insertSelective(equipmentRecordTVO);
        	if(stat > 0 && equipmentRecordTVO.getEquipmentRecordImgTList().size() > 0) {
        		
        		Iterator<String> fileNames = mRequest.getFileNames();
        		LocalDateTime nowdate = LocalDateTime.now();
    			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:SS");
				while(fileNames.hasNext()){
					MultipartFile file = mRequest.getFile(fileNames.next());
					Map<String,Object> urlMap = null;
	                try {
	                    urlMap = UploadFile.compressUploadedFile(file);
	                } catch (Exception e) {
	                    AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
	                }
	                TEquipmentRecordImgT equipmentRecordImgT = new TEquipmentRecordImgT();
					 equipmentRecordImgT.setCreateTime(nowdate.format(df));
					 equipmentRecordImgT.settEquipmentRecordTId(equipmentRecordTVO.getId());
					 equipmentRecordImgT.setTurl(urlMap.get("thumbnails")+"");
					 equipmentRecordImgT.setUrl(urlMap.get("fileName")+"");
					 this.equipmentRecordImgTMapper.insertSelective(equipmentRecordImgT);
				}
        	}
        	
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	return stat;
    }
    
    @Transient
    public int updateOne(TEquipmentRecordTVO equipmentRecordTVO,MultipartRequest mRequest) {
    	int stat = 0;
    	try {
    		stat = this.equipmentRecordTMapper.updateOne(equipmentRecordTVO);
    		
        	if(stat > 0 && equipmentRecordTVO.getEquipmentRecordImgTList().size() > 0) {
        		this.equipmentRecordImgTMapper.deleteMore(equipmentRecordTVO.getId());
        		Iterator<String> fileNames = mRequest.getFileNames();
        		LocalDateTime nowdate = LocalDateTime.now();
    			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:SS");
				while(fileNames.hasNext()){
					MultipartFile file = mRequest.getFile(fileNames.next());
					Map<String,Object> urlMap = null;
	                try {
	                    urlMap = UploadFile.compressUploadedFile(file);
	                } catch (Exception e) {
	                    AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
	                }
	                TEquipmentRecordImgT equipmentRecordImgT = new TEquipmentRecordImgT();
					 equipmentRecordImgT.setCreateTime(nowdate.format(df));
					 equipmentRecordImgT.settEquipmentRecordTId(equipmentRecordTVO.getId());
					 equipmentRecordImgT.setTurl(urlMap.get("thumbnails")+"");
					 equipmentRecordImgT.setUrl(urlMap.get("fileName")+"");
					 this.equipmentRecordImgTMapper.insertSelective(equipmentRecordImgT);
				}
        	}
        	
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	return stat;
    }
    
    @Transient
    public Object insertSelective1(TEquipmentRecordTVO equipmentRecordTVO,MultipartRequest mRequest,MultipartFile[] files) {
    	try {
    		 equipmentRecordTMapper.insertSelective(equipmentRecordTVO);
    		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  		   String dateString = formatter.format(new Date());
    		//保存附件
			Iterator<String> fileNames = mRequest.getFileNames();
			while(fileNames.hasNext()){
				MultipartFile file = mRequest.getFile(fileNames.next());
				Map<String,Object> urlMap = null;
                try {
                    urlMap = UploadFile.compressUploadedFile(file);
                } catch (Exception e) {
                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
                }
               
	                TEquipmentRecordImgT equipmentRecordImgT = new TEquipmentRecordImgT();
	                equipmentRecordImgT.setCreateTime(dateString);
	                equipmentRecordImgT.settEquipmentRecordTId(equipmentRecordTVO.getId());
	                equipmentRecordImgT.setTurl(urlMap.get("thumbnails")+"");
	                equipmentRecordImgT.setUrl(urlMap.get("fileName")+"");
					 this.equipmentRecordImgTMapper.insertSelective(equipmentRecordImgT);
				
        	}
        	
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	return AppUtil.returnMapJson("1", "保存成功!", null);
    	
    }
    
    @Transient
    public void updateOne1(TEquipmentRecordTVO equipmentRecordTVO,MultipartRequest mRequest,MultipartFile[] files ) {
    	try {
    		equipmentRecordTMapper.updateOne(equipmentRecordTVO);
    		
        		this.equipmentRecordImgTMapper.deleteMore(equipmentRecordTVO.getId());
        		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        		   String dateString = formatter.format(new Date());
        		//保存附件
    			Iterator<String> fileNames = mRequest.getFileNames();
    			while(fileNames.hasNext()){
    				MultipartFile file = mRequest.getFile(fileNames.next());
    				Map<String,Object> urlMap = null;
                        urlMap = UploadFile.compressUploadedFile(file);
                   
    	                TEquipmentRecordImgT equipmentRecordImgT = new TEquipmentRecordImgT();
    	                equipmentRecordImgT.setCreateTime(dateString);
    	                equipmentRecordImgT.settEquipmentRecordTId(equipmentRecordTVO.getId());
    	                equipmentRecordImgT.setTurl(urlMap.get("thumbnails")+"");
    	                equipmentRecordImgT.setUrl(urlMap.get("fileName")+"");
    					 this.equipmentRecordImgTMapper.insertSelective(equipmentRecordImgT);
				}
        	
        	
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	
    }

	public List<TCode> findEquipmentMaintType() {
		HashMap<String,Object> param = new HashMap<>();
		param.put("classify", "equipmentMaint");
		return codeMapper.findCodeByParam(param);
	}

	public Map<String, Object> getOne(Integer id) {
		Map<String, Object>	map = equipmentRecordTMapper.getOne(id);
		map.put("pic", equipmentRecordImgTMapper.selectImg(id));
		return map;
	}
    
}


